﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;
using System.Web.Configuration;
using Oracle.DataAccess.Client;
using System.Data;
using Telerik.Web.UI;

namespace sWarranty.WEB
{
    public partial class _1 : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        public string _currentUserRegion = "";

        //page nay chua xet theo curUserRegion

        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "SOMSerialMgr.aspx";

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");
            if (!IsPostBack)
            {

                txtToDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
                int dateTOShow = 0 - Convert.ToInt32(DateTime.Now.DayOfWeek) + 1; //hom nay thu dateTOShow, thi chi hien den cot D dateTOShow
                txtFromDate.Text = DateTime.Now.AddDays(dateTOShow).ToString("MM/dd/yyyy");
            }


        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    _currentUserRegion = u.Single().REGION_ID;
                    if (_currentUserRegion != "00")
                    {
                        DBAction.SetSelected(dropRegion, (u.Single().REGION_ID));
                        dropRegion.Enabled = false;
                    }
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }
        protected void BuildTable(DateTime from, DateTime to, string product, string model, string serial,
            string smsYN, string pndType, string shopName, string regID, string ureg)
        {
            DataTable dt = new DataTable();
            string st = from.ToString("yyyyMMdd") + "   " + to.ToString("yyyyMMdd") + "   " + product
                + "   " + model + "   " + serial + "   " + smsYN + "   " + pndType + "   " + shopName
                + "   " + regID + "   " + ureg;//for debug only
            dt = DBAction.runStore("PK_SOM_MST.PR_PROD_REG_PND_INQ", "uiroi", "p_ds_date", from.ToString("yyyyMMdd"), "p_de_date",
                to.ToString("yyyyMMdd"), "p_product", product, "p_model", model, "p_serial_no",
                serial, "p_sms_yn", smsYN, "p_pnd_type", pndType, "p_shop_name", shopName, "p_region_id", regID,
                "p_user_region", ureg, null, null, null, null, null, null, null, null, null, null);
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            Session["ProductRegPNDAspxData"] = from.ToString("yyyyMMdd") + "|" + to.ToString("yyyyMMdd") + "|" + product + "|"
                + model + "|" + serial + "|" + smsYN + "|" + pndType + "|" + shopName + "|" + regID + "|" + ureg;
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            DateTime from = Convert.ToDateTime(txtFromDate.Text);
            DateTime to = Convert.ToDateTime(txtToDate.Text);
            string sh = null;
            string p = null;
            string s = null;
            string sms = null;
            string mo = null;
            string pndT = "PND_SN_VL";
            string regID = "%";

            //if (drop.SelectedValue != "00")
            //    p = dropRegion.SelectedValue;
            if (dropBySMS.SelectedValue != "0")
                sms = dropBySMS.SelectedValue;
            if (!string.IsNullOrEmpty(txtSerial.Text.Trim()))
                s = txtSerial.Text.Trim().ToUpper(); ;
            if (!string.IsNullOrEmpty(txtModel.Text.Trim()))
                mo = txtModel.Text.Trim().ToUpper(); ;
            if (!string.IsNullOrEmpty(txtShopName.Text))
                sh = txtShopName.Text.Trim();
            if (dropRegion.SelectedValue != "00")
                regID = dropRegion.SelectedValue;

            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_PROD_REG_PND_INQ", "uiroi", "p_ds_date",
                from.ToString("yyyyMMdd"), "p_de_date", to.ToString("yyyyMMdd"), "p_product",
                p, "p_model", mo, "p_serial_no", s, "p_sms_yn", sms, "p_pnd_type",
                pndT, "p_shop_name", sh, "p_region_id", regID, "p_user_region", _currentUserRegion,
                null, null, null, null, null, null, null, null, null, null);
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            Session["ProductRegPNDAspxData"] = from.ToString("yyyyMMdd") + "|" + to.ToString("yyyyMMdd") + "|" + p + "|"
                + mo + "|" + s + "|" + sms + "|" + pndT + "|" + sh + "|" + regID + "|" + _currentUserRegion;

            RadGrid1.DataSource = dt;
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DateTime from = Convert.ToDateTime(txtFromDate.Text);
            DateTime to = Convert.ToDateTime(txtToDate.Text);
            string sh = null;
            string p = null;
            string s = null;
            string sms = null;
            string mo = null;
            string pndT = "PND_SN_VL";
            string regID = "%";

            //if (dropRegion.SelectedValue != "0")
            //    p = dropRegion.SelectedValue;
            if (dropBySMS.SelectedValue != "0")
                sms = dropBySMS.SelectedValue;
            if (!string.IsNullOrEmpty(txtSerial.Text.Trim()))
                s = txtSerial.Text.Trim().ToUpper(); ;
            if (!string.IsNullOrEmpty(txtModel.Text.Trim()))
                mo = txtModel.Text.Trim().ToUpper(); ;
            if (!string.IsNullOrEmpty(txtShopName.Text))
                sh = txtShopName.Text.Trim();
            if (dropRegion.SelectedValue != "00")
                regID = dropRegion.SelectedValue;

            BuildTable(from, to, p, mo, s, sms, pndT, sh, regID, _currentUserRegion);
        }
        protected void RadGrid1_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
        {
            if (e.Action == GridGroupsChangingAction.Group)
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = false;
            else
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = true;
        }

        protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            if (e.Column is GridBoundColumn)
            {
                (e.Column as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>"; //auto width
                if (e.Column.HeaderText == "AMOUNT")
                    (e.Column as GridBoundColumn).ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                if (e.Column.HeaderText == "PRODUCT")
                    (e.Column as GridBoundColumn).ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                if (e.Column.HeaderText == "CFM_FLAG")
                    (e.Column as GridBoundColumn).ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            }

        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.FileName = "data";
            RadGrid1.MasterTableView.ExportToExcel();
        }
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (_currentUserRegion != "00")
            {
                GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("REGION");
                if (column != null)
                {
                    column.Visible = false;
                }
            }
        }

    }
}